package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.HomePicListVO;
import com.chinairi.mall.model.HomePic;
import com.chinairi.mall.dao.HomePicMapper;
import com.chinairi.mall.service.IHomePicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.HomePicMO;
import com.chinairi.mall.util.ImageUtil;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-09-11
 */
@Service
public class HomePicServiceImpl extends ServiceImpl<HomePicMapper, HomePic> implements IHomePicService {
    @Autowired
    private HomePicMapper homePicMapper;

    /**
     * 配置图片库表
     *
     * @param homePic 图片数据对象
     */
    @Override
    public void config(HomePic homePic) {
        homePic.setHomePicCreator(UserSession.getUserId());
        homePic.setHomePicCreateTime(new Date());
        homePicMapper.insert(homePic);
    }

    /**
     * 更新图片库表
     *
     * @param homePic 图片数据对象
     */
    @Override
    public void update(HomePic homePic) {
        homePicMapper.updateById(homePic);
    }

    /**
     * 删除图片类型
     *
     * @param uuid 主键
     */
    @Override
    public void delete(String uuid) {
        homePicMapper.deleteById(uuid);
    }

    /**
     * 获取所有图片列表
     *
     * @return list
     */
    @Override
    public List<HomePic> get() {
        QueryWrapper<HomePic> wrapper = new QueryWrapper<>();
        return homePicMapper.selectList(wrapper);
    }

    /**
     * 点开首页后使用接口获取两个地方的图片配置
     *
     * @return
     */
    @Override
    public HomePicListVO getList() throws IOException {
        QueryWrapper<HomePic> wrapper = new QueryWrapper<>();
        wrapper
                .eq("HOME_PIC_TYPE_DBID","1");
        List<HomePic> homePicList1 = homePicMapper.selectList(wrapper);
        QueryWrapper<HomePic> wrapper2 = new QueryWrapper<>();
        wrapper2
                .eq("HOME_PIC_TYPE_DBID","2");
        List<HomePic> homePicList2 = homePicMapper.selectList(wrapper2);
        HomePicListVO homePicListVo = new HomePicListVO();
        homePicListVo.setAdPicList(homePicList1);
        homePicListVo.setFunPicList(homePicList2);
        return homePicListVo;
    }

}
